package com.exhr.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.exhr.biz.EmployeeBiz;
import com.exhr.pojo.Temployee;
import com.exhr.util.impl.Page;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends BaseAction{
	private EmployeeBiz empBiz;
	private Temployee emp;
	private File empPic;//上传图片
	private String empPicFileName;//上传文件名
	
	/**
	 * 添加员工信息 并把图片上传到服务器(功能未完成 拦截器 数据写入数据库)
	 * @return
	 */
	public String addEmployee(){
				
		try {
			InputStream in =new FileInputStream(empPic);
			
			String path=request.getRealPath("EmpPic");
			
			String filename=UUID.randomUUID()+empPicFileName;
			
			OutputStream out =new FileOutputStream(new File(path,filename));
			
			byte[] buffer = new byte[1024 * 1024];

			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			
			in.close();
			out.flush();
			out.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		empBiz.addEmp(emp);
		
		return SUCCESS;
	}
	
	/**
	 * 导出excel表(功能未完成 )
	 * @return
	 */
	public InputStream getExcel(){
		Temployee emp = new Temployee();
		Page page = new Page();
		page.setCurrentPage(1);
		page.setPageSize(20);
		List<Temployee> emps = empBiz.searchEmpForPage(emp, page);

		HSSFWorkbook wb = new HSSFWorkbook();//工作空间
		HSSFSheet sheet = wb.createSheet("Employee");//分页
		int i = 0;
		for (Temployee emp2 : emps) {
			HSSFRow row = sheet.createRow(i);//行
			HSSFCell cell = row.createCell(0);//单元格
			cell.setCellValue(emp2.getEmpid());//单元格内的值
			
			row.createCell(1).setCellValue(emp2.getEmpname());
			row.createCell(2).setCellValue(emp2.getTpost().getPosname());
			row.createCell(3).setCellValue(
					emp2.getTpost().getTdepartment().getDepname());

			i++;
		}
		
		ByteArrayOutputStream out=new ByteArrayOutputStream();
		
		try {
			
			wb.write(out);
			
//			FileOutputStream out = new FileOutputStream("d://text.xls");
//			wb.write(out);
//			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] arr=out.toByteArray();
		InputStream in=new ByteArrayInputStream(arr);
		
		return in;
	}

	public Temployee getEmp() {
		return emp;
	}

	public void setEmp(Temployee emp) {
		this.emp = emp;
	}

	public void setEmpBiz(EmployeeBiz empBiz) {
		this.empBiz = empBiz;
	}

	public File getEmpPic() {
		return empPic;
	}

	public void setEmpPic(File empPic) {
		this.empPic = empPic;
	}

	public String getEmpPicFileName() {
		return empPicFileName;
	}

	public void setEmpPicFileName(String empPicFileName) {
		this.empPicFileName = empPicFileName;
	}
	
}
